package robot.logging;

/**
 * @author David Ford
 * CS 424
 * ORANGS
 * 
 * This is the logging module for the ORANGS program.
 *
 */

import javax.swing.*;
import java.awt.*;

import java.io.*;

import robot.*;

public class LoggingPanel extends JPanel {
	
	private JEditorPane logView = new JEditorPane();
	private JScrollPane scrollWindow = new JScrollPane(logView);
	
	public LoggingPanel() throws IOException {
		this.add(scrollWindow);
		scrollWindow.setPreferredSize(new Dimension(1175, 85));
		logView.setEditable(false);
		this.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Log"), 
				BorderFactory.createEmptyBorder(3,3,3,3)));
		
		String logPath = "robot/logging/logs";
		File logFolder = new File(logPath);
		File[] fileList = logFolder.listFiles(); // makes an array of File type containing all the log file names
		
		String line; 
		
		
		StringBuffer sBuffer = new StringBuffer();
		
		for (int i=0; i < fileList.length; i++) {
			String logFiles = logPath + File.separator + fileList[i].getName();
			BufferedReader br = new BufferedReader(new FileReader(logFiles));
			
			if (fileList[i].isFile() && logFiles.endsWith(".txt")) {
									
				try {
					while ((line = br.readLine()) != null) {
						sBuffer.append(line + "\n");
					}
					br.close(); 
					logView.setText(sBuffer.toString());
	            }
	               catch (FileNotFoundException e) {
	               	logView.setText(e.getMessage());
	               }
				}
		}
	}	
}
